package com.itcc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import com.sina.sae.util.SaeUserInfo;

public class DBUtil {
	private static Connection conn = null;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	private static String username = SaeUserInfo.getAccessKey();
	private static String password = SaeUserInfo.getSecretKey();
	private static String appname = SaeUserInfo.getAppName();
	/**
	 * 数据库连接类
	 * @return
	 */
	public static Connection getConn() {
		String username = SaeUserInfo.getAccessKey();
		String password = SaeUserInfo.getSecretKey();
		String appname = SaeUserInfo.getAppName();
		String url = "jdbc:mysql://w.rdc.sae.sina.com.cn:3307/app_itccdemo?autoReconnect=true";
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		System.out.println("appname:" + appname);
		
		Connection conn = null;
		ResultSet rs = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e) {

			}
		}
		return conn;
	}

	public static ArrayList<User> search() {
		ArrayList<User> list = new ArrayList<User>();
		conn = getConn();
		String sql = "select *from usertb";
		try{
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				list.add(user);
			}
		}catch(Exception e){
			
		}
		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		getConn();
	}

}
